home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / COM / ProTERM Mac1.2a.sit / ProTERM Mac1.2a / Macros / Contributed / Chris' AWP Convert / Import AWP
Text File  |  1996-07-12  |  10KB  |  306 lines

  1. //
  2. // Translate AppleWorks WP files to ProTERM v1.3
  3. //
  4. // Written by Chris Budewig
  5. //  (c) 1995 A.C.E. Software
  6. //
  7. /////////////////////////////////////////////
  8. //
  9. // This macro will extract text and printer option data from AWP files,
  10. // translating printer options into ProTERM dot commands where there is
  11. // an equivalent.  The macro takes three optional arguments:  a source
  12. // file/folder, a destination folder, and a boolean value to activate
  13. // copying of text files.
  14. //
  15. // The source can be one of three forms:
  16. //  - Empty string ("") or omitted.  The user will be prompted to pick
  17. //    one or more files to be translated.
  18. //  - Pathname of a folder.  All AWP files in the specified folder will
  19. //    be translated.
  20. //  - Pathname of a file.  The file will be translated if an AWP file.
  21. //
  22. // The destination can be either:
  23. //  - Empty string ("") or omitted.  The user will be prompted for a
  24. //    destination directory.
  25. //  - Pathname.  Any filename is removed and path used for destination.
  26. //
  27. // The Copy Text parameter should be non-zero to copy text files and zero
  28. //    to ignore them.
  29. //
  30. /////////////////////////////////////////////
  31. //
  32. // Revision History:
  33. //
  34. // 1.3 - Upgraded status display
  35. // 1.2 - Added optional parm to allow copying text files.
  36. // 1.1 - Fixed bug with simultaneous LM and IN printer options.
  37. //       Added better error checking for file format version.
  38. // 1.0 - Initial version
  39. //
  40. /////////////////////////////////////////////
  41.  
  42. FUNC main()    //  [ STR fromPath [, STR toPath ] ] [, INT copyText]
  43. {
  44.   STR fromPath, toPath, files, nextFile, nextType;
  45.   INT copyText, next, last, AWPFile;
  46.  
  47.   // First get any supplied parameters
  48.   next = 1;
  49.   if is_str(argv(next)) {
  50.     fromPath = argv(next);
  51.     next = next + 1;
  52.   }
  53.   if is_str(argv(next)) {
  54.     toPath = argv(next);
  55.     next = next + 1
  56.   }
  57.   if is_num(argv(next)) && next <= argc() {
  58.     copyText = (argv(next) <> 0);
  59.     next = next + 1;
  60.   }
  61.   
  62.   // Resolve 'fromPath' to a list of files
  63.   if fromPath == "" {
  64.     files = fn_batch(1,"Import");
  65.   }
  66.   else {
  67.     if f_exists(fromPath) == "fldr" {
  68.       if str_right(fromPath,1) != ":" { fromPath = fromPath + ":"; }
  69.     }
  70.     files = fn_scan(fn_where(fromPath),"pdos","",fn_what(fromPath));
  71.   }
  72.   
  73.   if files != "" {
  74.     // Resolve destination path
  75.     if toPath == "" {
  76.       toPath = fn_path(1,"Save");
  77.       if toPath == "" { RETURN; }
  78.     }
  79.     else {
  80.       if f_exists(toPath) != "fldr" { toPath = fn_where(toPath); }
  81.     }
  82.     
  83.     // Process each file
  84.     next = 1; last = ARRAY_SIZE(files,1);
  85.     WHILE (next <= last) {
  86.       nextFile = FN_UNPK(ARRAY_GET(files,next));
  87.       fromPath = fn_where(nextFile);
  88.       nextType = f_exists(nextFile);
  89.       nextFile = fn_what(nextFile);
  90.       if copyText && nextType == "TEXT" {
  91.         CopyFile(fromPath+nextFile,toPath+nextFile);
  92.       }
  93.       nextType = STR_LEFT(nextType,2);
  94.  
  95.       // Filetypes can be in two forms: "1A  " for MacLinkPlus MEO Translators
  96.       // or "p^Zxx" (the ^Z is AWP filetype and the xx represents the auxtype)
  97.  
  98.       if nextType == "1A" || nextType == "p^Z" {  // ProDOS type = $1A
  99.         ImportAWP(fromPath+nextFile, toPath+nextFile);
  100.       }
  101.       next = next + 1;
  102.     }
  103.   }
  104.   END;
  105. }
  106.  
  107. ////////////////  Support functions  ///////////////
  108.  
  109. FUNC CopyFile(STR fromFile, toFile)
  110. {
  111.   INT from, to, win;
  112.  
  113.   win = StatProg(0,0,"Copying "+fn_what(fromFile)+"...","");
  114.   f_delete(toFile);
  115.   f_create(toFile);
  116.   from = f_open(fromFile,"RO");
  117.   to = f_open(toFile,"RW");
  118.   io_copy(from, to, "");
  119.   f_close(from);
  120.   f_close(to);
  121.   StatProg(win,100,"","");
  122.   RETURN;
  123. }
  124.  
  125. //
  126. // Read an AWP file and write a ProTERM text file
  127. //
  128. FUNC ImportAWP(STR fromFile, toFile)
  129. {
  130.   INT win, numLines, copied, char;
  131.   INT from, to, temp, position,byte0,byte1,len,CR,Ruler,More;
  132.   INT lm,lastLM,rm,lastRM,in,lastIN;
  133.   STR tempFile;
  134.   STR Stat1 = "Importing: " + fn_what(fromFile);
  135.   STR Stat2 = "Line: %d";
  136. //  STR stattmpl = fn_what(fromFile)+": line %d (%d%s)";
  137.   STR codes = "^a^b^c^d^e^f^g^h^i^j^k^l^m^n^o^p^q^r^s^t^u^v^w^x^y^z^[^¥^]^^^_";
  138.   
  139.   from = f_open(fromFile,"RO");
  140.   if from < 0 {
  141.     WIN_NOTE(1,str_format("Error %d opening %s!",from,fn_what(fromFile)));
  142.     RETURN;
  143.   }
  144.   if GetByte(from, 4) != 79 {
  145.     WIN_NOTE(1,fn_what(fromFile)+" is not an AWP file!");
  146.     f_close(from);
  147.     RETURN;
  148.   }
  149.   
  150.   switch GetByte(from, 183) {
  151.     case 0:  { Position = 300; }    // Normal starting position
  152.     case 30: { Position = 302; }    // AWP 3.0 file; skip first two bytes
  153.     default: {
  154.       WIN_NOTE(1,fn_what(fromFile)+" has an unrecognized version number!");
  155.       f_close(from);
  156.       RETURN;
  157.     }
  158.   }
  159.  
  160.   tempFile = toFile+" Temp";
  161.   f_delete(toFile);
  162.   f_create(toFile);
  163.   f_create(tempFile);
  164.   to = f_open(toFile,"RW");
  165.   temp = f_open(tempfile,"RW");
  166.   
  167.   lm = 0; lastLM = lm+10;        // Default ProTERM margins = 0
  168.   rm = 81; lastRM = rm-10;        // Default AWP margins = 1 inch
  169.   in = 0; lastIN = 0;            // and no indent
  170.   
  171.   win = 0;
  172.   numLines = 0; CR = 1;
  173.   More = 1;
  174.   while More {
  175.     // stattmpl = fn_what(fromFile)+": line %d (%d%s)";
  176.     //Status(str_format(stattmpl, numLines, 100*position/io_getlen(from),"%");
  177.     win = StatProg(win,100*position/io_getlen(from), Stat1,
  178.                                     STR_FORMAT(Stat2, numLines));
  179.     numLines = numLines + 1;
  180.     byte0 = GetByte(from, position);
  181.     byte1 = NextByte(from);
  182.     
  183.     switch byte1 {            // identify next record
  184.       case 0:{        // text line
  185.         if lm != lastLM {
  186.           lm = lastLM;
  187.           io_print(to, str_format("シlm%dシ^m",lm+in));
  188.         } 
  189.         if rm != lastRM {
  190.           rm = lastRM;
  191.           io_print(to, str_format("シrm%dシ^m",rm));
  192.         }
  193.         if in != lastIN {
  194.           in = lastIN;
  195.           io_print(to, str_format("シlm%dシシpm%dシ^m",lm+in,-in));
  196.         }
  197.         
  198.         Ruler = (NextByte(from) == 255);
  199.         len = NextByte(from);
  200.         CR = (len > 127);
  201.         if CR { len = len - 128; }
  202.         if Ruler { CR = 1 };            // Force CR with ruler
  203.         
  204.         copied = io_copy(from, temp, len);    // Copy the line
  205.         io_setlen(temp,copied);
  206.         io_setpos(temp,0);
  207.         
  208.         copied = 0;                // Translate codes
  209.         do {
  210.           copied = copied + io_copy(temp, to, codes);
  211.           char = GetByte(temp,copied-1);
  212.           if char < 32 {
  213.             io_setpos(to, io_getpos(to)-1);
  214.             switch char {
  215.               case 11: { io_print(to, "ハ"); }         // Code $0B = Sticky Spc
  216.               case 14: { io_print(to, "シdateシ"); }    // Code $0E = Print Date
  217.               case 15: { io_print(to, "シtimeシ"); }    // Code $0F = Print Time
  218.               case 22: { io_print(to, "^I"); }        // Code $16 = Tab
  219.             }
  220.           }
  221.         } while copied < len;
  222.         io_setpos(temp,0);
  223.         if CR {io_print(to, "^m"); }
  224.       }
  225.       case 208: {        // carriage return
  226.         io_print(to, str_repeat(' ', byte0)+"^m");
  227.       }
  228.       case 215: {        // right justified
  229.         io_print(to, "シrjシ^m");
  230.       }
  231.       case 217: {        // set left margin
  232.         lastLM = byte0;
  233.       }
  234.       case 218: {        // set right margin
  235.         lastRM = 81 - byte0;
  236.       }
  237.       case 222: {        // set indent
  238.         lastIN = byte0;
  239.       }
  240.       case 224: {        // left justified
  241.         io_print(to, "シljシ^m");
  242.       }
  243.       case 225: {        // center justified
  244.         io_print(to, "シcjシ^m");
  245.       }
  246.       case 230: {        // single space
  247.         io_print(to, "シssシ^m");
  248.       }
  249.       case 231: {o_prioo_print2: {o$        ^Eo(to, print2}
  250.       ca: {o$f      }
  251.   _print(to(s/ single sp   ca:     case 217: {        // set left margin
  252.         lastLM = byte0;
  253.       }
  254.       case 218: {        // set right margin
  255.         lastRM = 81 - byte0;
  256.       }
  257.       case 222: {        // set indent
  258.         lastIN = byte0;
  259.       }
  260.       case 224: {        // left justified
  261.         io_print(to, "シljシ^m");
  262.       }
  263.       case 225: {        // center justified
  264.         io_print(toase S^ft      case 2(to(snt Time
  265.          pgt ijシ^m";
  266.  )     case 2(eR_astIN {
  267.           in =Rrpgt ijシ^f)wO      lasR_astIN }    en,_astIN {
  268. int(tkbytdc0   ii18:io_prinN"i {
  269. intP  ltn^ og posFile)+" is- ] ] [,unR2t^oTERM tOAyRo;wileent
  270.   ype;
  271.   INT copurint th, filesN1(,d1Iuslwilt (|
  272.   ycn(from) =;
  273.  t+(tkbyt   }
  274.  / is),d1Iuslwilt e% str_repeat(l=] ] [ /">io_prin9"slwilt e%i= by1)}    // No is),d1Iuo$    1)}    //%i=      F[ /">i str_rr_rr_rr_rr_rrr"li "romFile,"RO No i
  275. +in Time
  276.          pgt ijシ^m";
  277.  )    "li "=dcn(from) =;
  278.  t+(tkbyt  mp,copied);
  279.         io_setpos(temtkbyt  mp,copie      pgt q   +)o i
  280. +in n%  lastede kbyt  mp,copied);
  281.   {        // s.<   i+s_rr_tif m";
  282.  ) n%no str_rr{        /opiet q {        //edTR tempFile;
  283. eam  c        /Fileint th,e      1gyシis),d0f rm oT    lm =Filw(
  284. +ii224     ca_prio1 -    /Fil "シss^ n%  lastede kb^lw(
  285. +ii     e      1gyシi" C ImportA n%^f0.-    /Filnudt"r_rr_tif m";
  286.  ) r_tif m";
  287.  ) n%no strrn   1gyシiosser w  1gyシis),d0f rm oT    lm =Ft
  288.   lt       oT    l11Ebk2N(+i)IcNEn n%  laU   }
  289.   /////gg       la iffn
  290.  0f rtsM;
  291.    nr(argv       pgt ijシgv s  lm =IN {
  292. a,e  s),d0/tsM   i+s_rr_tif m";
  293. hgv   c m" {        /, m";f_0/tsM   ro, '    //pif m";
  294. hgv sRs),dif m"h}hgv<p   ca:     cas+ io_setpos(u_prinByte(from, 4) != 79 {
  295.     Wouor_tif m";
  296.  ) n%no strr)aliossL"oxtB^C   
  297.  stified
  298.       1)}                        E,;Ft    Pa/tsM   i+u= 79 {
  299.     Wouor_tif m";
  300.  ) n%no strr)aliosw  Wouor_t  
  301.  s/_elt th, 
  302.  s/_eNEnFo( prinN"i {
  303. intP 2o_setpos(u_p;     c!yシiosser w  1go$    1)}    //m  c        /Filsaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx and no indxxxxs n+xxxx_    /Fil " ltslt eシexxxstified
  304.       1    }tsMxxxxxxxxxxxxxxx eシexxxstified
  305.  sxxsyC~gt ixxxxxxle,1Ch}hgv<p   Da 5: l 1p
  306.     ehi    //m  c^xxxxstifiシ)d+{ (VLA=variabl